/**
 * 
 */





InvoicePage = {
		'BindEvent': function(){
			$('#btnSubmit').bind('click', function(){
				InvoicePage.BindPageInfo();
			});
			
			$('#allCheck').bind('click', function(){ 
			  if(this.checked==true){
				 $('input[name="checkOneBox"]').each(function(){
				    	this.checked =true;
				 }); 
			  }else{
					 $('input[name="checkOneBox"]').each(function(){
					    	this.checked =false;
					 });   
			  }
			});
			
			//纸质发票
			$('#paperInvoice').bind('click', function(){
				
				var chk_value =[]; 
				var error_msg = "";
				$('input[name="checkOneBox"]:checked').each(function(){ 
					chk_value.push($(this).attr('tid')); 
					if($(this).attr('cash_back_status')=='0'){
						error_msg += $(this).attr('tid')+'订单没有上传返现，不可开发票\r\n';
					}
				}); 
				if(error_msg != ""){
					alert(error_msg);
					return false;
				}
				
				if(chk_value.length==0){
					alert('请选择要开票的订单！'); 
				}else{
				  index = $(this).attr('index');
					var result = InvoiceService.ApplyInvoice(chk_value,22);
					if(result.IsSuccess == 1){
						alert(result.Result);
						InvoicePage.BindPageInfo();
					}
				}
				
			});
			//纸质发票专票
			$('#specialPaperInvoice').bind('click', function(){
				var chk_value =[]; 
				var error_msg = "";
				$('input[name="checkOneBox"]:checked').each(function(){ 
					chk_value.push($(this).attr('tid')); 
					if($(this).attr('cash_back_status')=='0'){
						error_msg += $(this).attr('tid')+'订单没有上传返现，不可开发票\r\n';
					}
				}); 
				if(error_msg != ""){
					alert(error_msg);
					return false;
				}
				
				if(chk_value.length==0){
					alert('请选择要开票的订单！'); 
				}else{
				  index = $(this).attr('index');
					var result = InvoiceService.ApplyInvoice(chk_value,23);
					if(result.IsSuccess == 1){
						alert(result.Result);
						InvoicePage.BindPageInfo();
					}
				}
				
			});
			//电子发票
			$('#electronicInvoice').bind('click', function(){
				var chk_value =[]; 
				var error_msg = "";
				$('input[name="checkOneBox"]:checked').each(function(){ 
					
					chk_value.push($(this).attr('tid')); 
					if($(this).attr('cash_back_status')=='0'){
						error_msg += $(this).attr('tid')+'订单没有上传返现，不可开发票\r\n';
					}
				}); 
				if(error_msg != ""){
					alert(error_msg);
					return false;
				}
				if(chk_value.length==0){
					alert('请选择要开票的订单！');
				}else{
				  index = $(this).attr('index');
					var result = InvoiceService.ApplyInvoice(chk_value,21);
					if(result.IsSuccess == 1){
						alert(result.Result);
						InvoicePage.BindPageInfo();
					}
				}
				
			});
			
		},
		'BindTable': function(result){
			lst_order_en = result.datas;
			
			var html = '';
			for(var index in lst_order_en){
				var order_en = lst_order_en[index];
				var tr_html = "<tr>";
				tr_html += "<td>" + "<input type='checkbox' cash_back_status='"+order_en.cash_back_status+"' tid='" + order_en.order_id + "' style='position:relative;width: 50px; opacity: 100;' name='checkOneBox'>"+"</td>";
				tr_html += "<td>" + order_en.third_order_id +"<br /><a target='_blank' href='/Order/orderdetail?order_id="+ order_en.order_id +"'>" + order_en.order_id + "</a></td>";
				tr_html += "<td>" + InvoicePage.ConvertOrderType(order_en.order_type) + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.buyer_alipay_no) + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.buyer_nick) + "</td>"
				tr_html += "<td>" +  InvoicePage.ConvertStrNull(order_en.buyer_message)  + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.seller_memo) + "</td>"

				if(order_en.invoice_title !=null && order_en.invoice_title !='' &&order_en.invoice_title !=undefined){
					tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.invoice_title)  + "</td>"
				}else{
					tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.invoice_name)  + "</td>"
				}
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.invoice_amount) + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.consignee_name) + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.goods_amount) + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.payment_amount) + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.freight) + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.discount_fee) + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.order_amount) + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertStrNull(order_en.income_amount) + "</td>"
				tr_html += "<td>" + order_en.order_date + "</td>"
				tr_html += "<td>" + InvoicePage.ConvertCashBackStatus(order_en.cash_back_status)+ "</td>"
				tr_html += "</tr>";
				html += tr_html;
			} 
			$('#tbl_order_info tbody').html('');
			$('#tbl_order_info tbody').append(html);
			
			var ul= "<ul class=\"pager\">";
			    ul+="<li id='page_first' index='1'>";
			    ul+="<a href=\"#\"  id='page_first' index='1'>首页</a>";
			    ul+="</li>"
				ul+="<li>";
			    ul+="<a href=\"#\"  id='page_prov' index='"+result.preIndex+"'>← 上一页</a>";
			    ul+="</li>"
			    ul+="<li>";
			    ul+="<a href=\"#\" id='page_next' index='"+result.nextIndex+"'>下一页 →</a>";
			    ul+="</li>"
				ul+="<li>";
			    ul+="<a href=\"#\" id='page_last' index='"+result.totalPage+"'>尾页</a>";
			    ul+="</li>"
				ul+="<li>";
			    ul+="共"+result.totalCount+"条 共"+result.totalPage+"页 当前在第"+result.index+"页";
			    ul+="<a  href=\"#\" id='page_jump'>"+"跳转 到</a><input id='jump_Number' type=\"number\" style=\"width:50px;\">页";
			    ul+="</li>"
			    ul+= "</ul>";
			
			$('#div_pageInfo ul').html('');
			$('#div_pageInfo ul').html(ul);

			InvoicePage.BindPageEvent();
		
		},
		'BindPageEvent': function(){
			
			var thirdOrderIds = $('#thirdOrderIds').val();
			var orderStartDate = $('#orderStartDate').val();
			var orderEndDate = $('#orderEndDate').val();
			
			//是否有返现
			var cashBackStatus = $('#cash_back_status').val();
			// 存在留言
			var message = document.getElementById("buyer_message").checked;
			
			var orderType = $('#orderType').val();
			
			var index =1;
			
			$('#page_first').bind('click', function(){
				  index = $(this).attr('index');
					var result = InvoiceService.Invoice(thirdOrderIds,
							orderStartDate,
							orderEndDate,cashBackStatus,message,orderType,index);
					InvoicePage.BindTable(result.Result);
			});
			
			$('#page_prov').bind('click', function(){
				 index = $(this).attr('index');
					var result = InvoiceService.Invoice(thirdOrderIds,
							orderStartDate,
							orderEndDate,cashBackStatus,message,orderType,index);
					InvoicePage.BindTable(result.Result);
			});
			
			$('#page_next').bind('click', function(){
    				 index = $(this).attr('index');
 					var result = InvoiceService.Invoice(thirdOrderIds,
							orderStartDate,
							orderEndDate,cashBackStatus,message,orderType,index);
    					InvoicePage.BindTable(result.Result);
			});
			
			$('#page_last').bind('click', function(){
				 index = $(this).attr('index');
					var result = InvoiceService.Invoice(thirdOrderIds,
							orderStartDate,
							orderEndDate,cashBackStatus,message,orderType,index);
					InvoicePage.BindTable(result.Result);
			});
			
			$('#page_jump').bind('click',function(){
				
				 index = $('#jump_Number').val();
					var result = InvoiceService.Invoice(thirdOrderIds,
							orderStartDate,
							orderEndDate,cashBackStatus,message,orderType,index);
					InvoicePage.BindTable(result.Result);
			});
			
		},
		'ConvertStrNull':function(obj){
			
			if(obj == null){
				return '';
			}
			else{
				return obj;
			}
		},
		'ConvertStatus':function(obj){
			if(obj == 'TRADE_FINISHED'){
				return '订单完成';
			}else if(obj =='TRADE_CLOSED_BY_TAOBAO'){
				return '淘宝关闭订单';
			}else if(obj =='TRADE_CLOSED'){
				return '订单关闭';
			}else if(obj =='WAIT_SELLER_SEND_GOODS'){
				return '待发货';
			}else if(obj =='WAIT_BUYER_CONFIRM_GOODS'){
				return '待收货';
			}else if(obj =='WAIT_BUYER_PAY'){
				return '待付款';
			}
		},		
		'ConvertWhaleyStatus':function(obj){
			if(obj == '10'){
				return '已提交';
			}else if(obj =='11'){
				return '已审核';
			}else if(obj =='12'){
				return '订单处理中';
			}else if(obj =='21'){
				return '待支付';
			}else if(obj =='22'){
				return '已支付';
			}else if(obj =='25'){
				return '物流处理中';
			}else if(obj =='31'){
				return '已发货';
			}else if(obj =='32'){
				return '已签收';
			}else if(obj =='35'){
				return '已完成';
			}else if(obj =='41'){
				return '退货中';
			}else if(obj =='42'){
				return '退货完成';
			}else if(obj =='43'){
				return '换货中';
			}else if(obj =='44'){
				return '换货完成';
			}else if(obj =='51'){
				return '用户取消';
			}else if(obj =='52'){
				return '客服取消';
			}else if(obj =='53'){
				return '系统自动取消';
			}
		} ,
		'BindPageInfo':function(){
			var thirdOrderIds = $('#thirdOrderIds').val();
			var orderStartDate = $('#orderStartDate').val();
			var orderEndDate = $('#orderEndDate').val();
			
			//是否有返现
			var cashBackStatus = $('#cash_back_status').val();
			// 存在留言
			var message = document.getElementById("buyer_message").checked;
			
			var orderType = $('#orderType').val();
			
			var result = InvoiceService.Invoice(thirdOrderIds,
					orderStartDate,
					orderEndDate,cashBackStatus,message,orderType,1,100);
			if(result.IsSuccess == 0){
				alert(result.ErrorMsg);
			}
			InvoicePage.BindTable(result.Result);
		} ,
		'ConvertCashBackStatus':function(obj){
			if(obj == '0'){
				return '未导入';
			} 
			return '已导入';
		}    ,
		'ConvertOrderType':function(obj){
			if(obj == '1'){
				return '天猫';
			} else if(obj =='2'){
				return '京东';
			}else if(obj =='3'){
				return '官网';
			}else if(obj =='5'){
				return '英雄会';
			}else if(obj =='6'){
				return '村淘';
			}else if(obj =='7'){
				return '天猫企业采购';
			}else if(obj =='8'){
				return '经销商';
			}
			return '未分类';
		} 
		
		
};


$(document).ready(function(){
	InvoicePage.BindEvent();
});


